﻿using SenGe.Domain;
using SenGe.Domain.Utils;
using SenGe.Infrastructure;
using Yitter.IdGenerator;

namespace SenGe.Application
{
    /// <summary>
    /// 用户相关服务
    /// </summary>
    public class SenUserService : BaseService, ISenUserService
    {
        /// <summary>
        /// 用户仓储接口
        /// </summary>
        private readonly ISenUserRepository _userRepository;
        /// <summary>
        /// 构造函数引入依赖注入
        /// </summary>
        /// <param name="userRepository">用户仓储</param>
        public SenUserService(ISenUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        /// <summary>
        /// 检查数据库链接
        /// </summary>
        /// <returns></returns>
        public async Task<bool> CheckDbConnection()
        {
            return await _userRepository.CheckDb();
        }

        /// <summary>
        /// 检查注册信息
        /// </summary>
        /// <returns></returns>
        public async Task<UserRegisterCheckResult> CheckRegisterInfo(UserRegisterModel model)
        {
            if (model == null || string.IsNullOrEmpty(model.Name) || string.IsNullOrEmpty(model.UserName)) return UserRegisterCheckResult.DataIsEmpty;
            return await _userRepository.CheckRegisterInfo(model);
        }

        /// <summary>
        /// 通过编号获取用户注册模型
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns></returns>
        public async Task<UserRegisterModel?> GetUserRegisterModel(long id)
        {
            var dbItem = await _userRepository.GetByIdAsync(id);
            if (dbItem != null)
            {
                return new UserRegisterModel()
                {
                    Id = dbItem.Id,
                    Name = dbItem.Name,
                    UserName = dbItem.UserName,
                    Password = string.Empty,
                    ConfirmPassword = string.Empty,
                    Avatar = dbItem.Avatar,
                    Phone = dbItem.Phone,
                    TrueName = dbItem.TrueName,
                    Gender = dbItem.Gender,
                    Birthday = dbItem.Birthday,
                    IdCard = dbItem.IdCard,
                    Address = dbItem.Address,
                    Remarks = dbItem.Remarks
                };
            }
            return null;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="model">用户模型</param>
        /// <returns></returns>
        public async Task<bool> AddOrUpdateUser(UserRegisterModel model)
        {
            SenUser? saveItem = null;
            if (model.Id == 0)
            {
                //新增
                saveItem = new SenUser()
                {
                    Id = YitIdHelper.NextId(),
                    Name = model.Name,
                    UserName = model.UserName,
                    PassWord = SecurePasswordHasher.HashPassword(model.Password),
                    Phone = model.Phone,
                    Gender = model.Gender,
                    TrueName = model.TrueName,
                    Birthday = model.Birthday,
                    IdCard = model.IdCard,
                    Address = model.Address,
                    Remarks = model.Remarks,
                    CreatedUserId = model.UpdatedUserId,
                    CreatedUserName = model.UpdatedUserName,
                    CreatedTime = DateTime.Now,
                    UpdatedUserId = model.UpdatedUserId,
                    UpdatedUserName = model.UpdatedUserName,
                    UpdatedTime = DateTime.Now,
                };
                if (model.AvatarFile != null)
                {
                    //处理头像

                }
                return await _userRepository.InsertAsync(saveItem);
            }
            else
            {
                //编辑
                saveItem = await _userRepository.GetByIdAsync(model.Id);
                if (saveItem == null) return false;
                saveItem.PassWord = SecurePasswordHasher.HashPassword(model.Password);
                saveItem.Phone = model.Phone;
                saveItem.Gender = model.Gender;
                saveItem.TrueName = model.TrueName;
                saveItem.Birthday = model.Birthday;
                saveItem.IdCard = model.IdCard;
                saveItem.Address = model.Address;
                saveItem.Remarks = model.Remarks;
                saveItem.UpdatedUserId = model.UpdatedUserId;
                saveItem.UpdatedUserName = model.UpdatedUserName;
                saveItem.UpdatedTime = DateTime.Now;
                if (model.AvatarFile != null)
                {
                    //处理头像

                }
                return await _userRepository.UpdateAsync(saveItem);
            }
        }

        /// <summary>
        /// 通过登录用户名获取用户Dto
        /// </summary>
        /// <param name="userName">登录用户名</param>
        /// <returns></returns>
        public async Task<DtoSenUser?> GetUserByUserName(string userName)
        {
            return await _userRepository.GetUserByUserName(userName);
        }

        /// <summary>
        /// 通过用户编号获取用户Dto
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns></returns>
        public async Task<DtoSenUser?> GetUserById(long id)
        {
            SenUser? dbItem = await _userRepository.GetByIdAsync(id);
            if (dbItem != null)
            {
                return new DtoSenUser()
                {
                    Id = dbItem.Id,
                    Name = dbItem.Name,
                    UserName = dbItem.UserName,
                    PassWord = dbItem.PassWord
                };
            }
            return null;
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="user">用户模型</param>
        /// <returns></returns>
        public async Task<bool> ChangePassWord(DtoSenUser user)
        {
            return await _userRepository.ChangePassWord(user);
        }

        /// <summary>
        /// 更新最新登录时间
        /// </summary>
        /// <param name="user">用户模型</param>
        /// <returns></returns>
        public async Task<bool> UpdateLastLoginTime(DtoSenUser user)
        {
            return await _userRepository.UpdateLastLoginTime(user);
        }
    }
}
